<?php

class DynamicWebAction extends Action {
		
	public function getDynWebList($order="updateTime", $orderBy="desc", $start = 0, $limit = 25) {
				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$DynWebModel = M("module_dynamic_web");
		$cid = Session::$user["customerId"];
		
		//$condition["deleted"] = array("eq",0);
		$arr = $DynWebModel->order("$order $orderBy")->limit("$start, $limit")->select();
		if ($arr) {
			$count = $DynWebModel->count();
			Output::instance()->objArray($arr, $count);
		} else {
			Output::instance()->resultCode(code_success);
		}
		
		
	}	
	
	
	public function addDynWeb($name = NULL,$tagNames = NULL,$pageUrl = NULL,$shareTitle = NULL,$shareDescript = NULL,$sharePicUrl = NULL,$bgMusicUrl = NULL,$memo = NULL){
				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$DynWebModel = M("module_dynamic_web");
		$cid = Session::$user["customerId"];
		
		$time = date("Y-m-d H:i:s");
		$data["name"] = $name;
		$data["tagNames"] = $tagNames;
		//-----ihziluoh添加标签
		if(!empty($tagNames)){
			addAccessTags($tagNames);
		}
		//-----ihziluoh添加标签
		$data["pageUrl"] = $pageUrl;
		$data["shareTitle"] = $shareTitle;
		$data["shareDescript"] = $shareDescript;
		$data["sharePicUrl"] = $sharePicUrl;
		$data["bgMusicUrl"] = $bgMusicUrl;
		$data["memo"] = $memo;

        $data["createTime"] = $time;
        $data["updateTime"] = $time;

		$result = $DynWebModel->add($data);
		if ($result) {
		  $data["id"] = $result;
		  Output::instance()->object($data);
		} else {
		  Output::instance()->resultCode(code_sql_error);
		}		
	}
	
	public function updateDynWeb($id,$name = NULL,$tagNames = NULL,$pageUrl = NULL,$shareTitle = NULL,$shareDescript = NULL,$sharePicUrl = NULL,$bgMusicUrl = NULL,$memo = NULL){
				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}		
		
		$DynWebModel = M("module_dynamic_web");
		$cid = Session::$user["customerId"];
		
		$time = date("Y-m-d H:i:s");
		$data["name"] = $name;
		$data["tagNames"] = $tagNames;
		//-----ihziluoh添加标签
		if(!empty($tagNames)){
			addAccessTags($tagNames);
		}
		//-----ihziluoh添加标签
		$data["pageUrl"] = $pageUrl;
		$data["shareTitle"] = $shareTitle;
		$data["shareDescript"] = $shareDescript;
		$data["sharePicUrl"] = $sharePicUrl;
		$data["bgMusicUrl"] = $bgMusicUrl;
		$data["memo"] = $memo;
		$data["updateTime"] = $time;

		$result = $DynWebModel->where("id=$id")->save($data);
		if ($result !== false) {
		  $data["id"] = $id;
		  Output::instance()->object($data);
		} else {
		  Output::instance()->resultCode(code_sql_error);
		}		
	}
    
	public function getWebById($id){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$WebModel=M("module_dynamic_web");	
	
		$cid = Session::$user["customerId"];
		$condition["id"] = array("eq",$id);
		$web = $WebModel->where($condition)->find();
		if ($web) {
			Output::instance()->object($web);
		} else {
			Output::instance()->resultCode(code_success);
		}
	}
	
	public function deleteDynWeb($id){		
				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$DynWebModel = M("module_dynamic_web");
		$result = $DynWebModel->where("id=$id")->delete();		
		if ($result) {
			Output::instance()->resultCode(code_success);			
		} else {
			Output::instance()->resultCode(code_sql_error);
		}
	}
	
	
	//得到网站页面的列表
	public function getWebPageList($webId,$order="id", $orderBy="desc", $start = 0, $limit = 25){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$cid = Session::$user["customerId"];				
		
		$WebPageElementModel=M("module_dynamic_web_page");
		$condition["webId"] = array('eq',$webId);
		$arr = $WebPageElementModel->where($condition)->order("$order $orderBy")->limit("$start, $limit")->select();
		if ($arr) {
			$count=$WebPageElementModel->where($condition)->count();
			Output::instance()->objArray($arr,$count);
		} else {
			Output::instance()->resultCode(code_success);
		}
	}	
	
	//添加网站页面
	public function addWebPage($webId = NULL,$pageName = NULL,$sequence = NULL,$bgUrl = NULL,$memo = NULL){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$WebPageModel = M("module_dynamic_web_page");
		$cid = Session::$user["customerId"];
		
		$time = date("Y-m-d H:i:s");
		$data["webId"] = $webId;
		$data["pageName"] = $pageName;
		$data["sequence"] = $sequence;
		$data["bgUrl"] = $bgUrl;
		$data["memo"] = $memo;
        $data["createTime"] = $time;
        $data["updateTime"] = $time;

		$result = $WebPageModel->add($data);
		if ($result) {
		  $data["id"] = $result;
		  Output::instance()->object($data);
		} else {
		  Output::instance()->resultCode(code_sql_error);
		}		
	}
	
	
	//修改网站页面
	public function updateWebPage($id,$webId = NULL,$pageName = NULL,$sequence = NULL,$bgUrl = NULL,$memo = NULL){
				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}		
		
		$WebPageModel = M("module_dynamic_web_page");
		$cid = Session::$user["customerId"];
		
		$time = date("Y-m-d H:i:s");
		$data["webId"] = $webId;
		$data["pageName"] = $pageName;
		$data["sequence"] = $sequence;
		$data["bgUrl"] = $bgUrl;
		$data["memo"] = $memo;
		$data["updateTime"] = $time;

		$result = $WebPageModel->where("id=$id")->save($data);
		if ($result !== false) {
		  $data["id"] = $id;
		  Output::instance()->object($data);
		} else {
		  Output::instance()->resultCode(code_sql_error);
		}		
	}
	
	public function getWebPageById($id){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$WebPageModel=M("module_dynamic_web_page");	
	
		$cid = Session::$user["customerId"];
		$condition["id"] = array("eq",$id);
		$webPage = $WebPageModel->where($condition)->find();
		if ($webPage) {
			Output::instance()->object($webPage);
		} else {
			Output::instance()->resultCode(code_success);
		}
	}
	
	//删除网站页面
	public function deleteWebPage($id){		
				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$WebPageElementModel = M("module_dynamic_web_page_element");
		$resultElement = $WebPageElementModel->where("webPageId=$id")->delete();
		
		$WebPageModel = M("module_dynamic_web_page");
		$resultPage = $WebPageModel->where("id=$id")->delete();		
		if ($resultPage) {
			Output::instance()->resultCode(code_success);			
		} else {
			Output::instance()->resultCode(code_sql_error);
		}
	}
	
	
	//得到页面元素的列表
	public function getWebPageElementList($webPageId){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$cid = Session::$user["customerId"];				
				
		$WebPageElementModel=M("module_dynamic_web_page_element");
		$condition["webPageId"] = array('eq',$webPageId);
		$arr = $WebPageElementModel->where($condition)->select();
		if ($arr) {
			Output::instance()->objArray($arr);
		} else {
			Output::instance()->resultCode(code_success);
		}
	}	
	
	
	//添加页面元素
	public function addWebPageElement($webPageId = NULL,$elementType = "pic",$text = NULL,$picUrl = NULL,$width = NULL,$height = NULL,$loadMode = NULL,
                                      $slideDirect = NULL,$loadDisplay = NULL,$href = NULL,$topPos = NULL,$leftPos = NULL,$sizeUnit=NULL,$posUnit=NULL,$marginUnit=NULL,$topMargin=NULL,$bottomMargin=NULL){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$WebPageElementModel = M("module_dynamic_web_page_element");
		$cid = Session::$user["customerId"];
		
		$time = date("Y-m-d H:i:s");
		$data["webPageId"] = $webPageId;
		$data["elementType"] = $elementType;
		$data["text"] = $text;
		$data["picUrl"] = $picUrl;
		$data["width"] = $width;
		$data["height"] = $height;
		$data["loadMode"] = $loadMode;
		$data["slideDirect"] = $slideDirect;
		$data["loadDisplay"] = $loadDisplay;
		$data["href"] = $href;
		$data["topPos"] = $topPos;
		$data["leftPos"] = $leftPos;
		
		$data["sizeUnit"] = $sizeUnit;
		$data["posUnit"] = $posUnit;
		$data["marginUnit"] = $marginUnit;
		$data["topMargin"] = $topMargin;
		$data["bottomMargin"] = $bottomMargin;
		
        $data["createTime"] = $time;
        $data["updateTime"] = $time;

		$result = $WebPageElementModel->add($data);
		if ($result) {
		  $data["id"] = $result;
		  Output::instance()->object($data);
		} else {
		  Output::instance()->resultCode(code_sql_error);
		}		
	}
	
	//修改页面元素
	public function updateWebPageElement($id,$webPageId = NULL,$elementType = "pic",$text = NULL,$picUrl = NULL,$width = NULL,$height = NULL,$loadMode = NULL,
                                      $slideDirect = NULL,$loadDisplay = NULL,$href = NULL,$topPos = NULL,$leftPos = NULL,$sizeUnit=NULL,$posUnit=NULL,$marginUnit=NULL,$topMargin=NULL,$bottomMargin=NULL){
				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}		
		
		$WebPageElementModel = M("module_dynamic_web_page_element");
		$cid = Session::$user["customerId"];
		
		$time = date("Y-m-d H:i:s");
		$data["webPageId"] = $webPageId;
		$data["elementType"] = $elementType;
		$data["text"] = $text;
		$data["picUrl"] = $picUrl;
		$data["width"] = $width;
		$data["height"] = $height;
		$data["loadMode"] = $loadMode;
		$data["slideDirect"] = $slideDirect;
		$data["loadDisplay"] = $loadDisplay;
		$data["href"] = $href;
		$data["topPos"] = $topPos;
		$data["leftPos"] = $leftPos;
		
		$data["sizeUnit"] = $sizeUnit;
		$data["posUnit"] = $posUnit;
		$data["marginUnit"] = $marginUnit;
		$data["topMargin"] = $topMargin;
		$data["bottomMargin"] = $bottomMargin;
		$data["updateTime"] = $time;
		
		$result = $WebPageElementModel->where("id=$id")->save($data);		
		if ($result !== false) {
		  $data["id"] = $id;
		  Output::instance()->object($data);
		} else {
		  Output::instance()->resultCode(code_sql_error);
		}		
	}
	
	public function getWebPageElementById($id){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$WebPageElementModel=M("module_dynamic_web_page_element");	
	
		$cid = Session::$user["customerId"];
		$condition["id"] = array("eq",$id);
		$webPageElement = $WebPageElementModel->where($condition)->find();
		if ($webPageElement) {
			Output::instance()->object($webPageElement);
		} else {
			Output::instance()->resultCode(code_success);
		}
	}
		
	public function deleteWebPageElement($id){		
				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$WebPageElementModel = M("module_dynamic_web_page_element");
		$result = $WebPageElementModel->where("id=$id")->delete();		
		if ($result) {
			Output::instance()->resultCode(code_success);			
		} else {
			Output::instance()->resultCode(code_sql_error);
		}
	}
	
}
